package org.jeecg.modules.business.job.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.business.job.service.SyncService;
import org.jeecg.modules.dock.entity.dto.request.MasterDataRequestDTO;
import org.jeecg.modules.dock.service.SapDockingRepeaterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;

/*
 * 项目名称:    wms-server-jeecg
 * 接口描述:    //
 * 创建人员:    zhaojun
 * 创建时间:    2023-11-08
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class SyncServiceImpl implements SyncService {

    @Autowired
    private SapDockingRepeaterService sapDockingRepeaterService;

    @Value("${transtella.bus.factory}")
    private String factory;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void asycCustomer() {
        MasterDataRequestDTO masterDataRequestDTO2 = new MasterDataRequestDTO();
        masterDataRequestDTO2.setMasterDataType("05");
        masterDataRequestDTO2.setMaterialType(factory);
        masterDataRequestDTO2.setUuid(IdUtil.fastSimpleUUID());
        masterDataRequestDTO2.setCreateTime(DateUtil.formatDate(new Date()).replace("-", ""));
        sapDockingRepeaterService.sapMaster(masterDataRequestDTO2);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void syncMaterial(Integer beforeDay) {
        if (ObjectUtil.isEmpty(beforeDay)) {
            MasterDataRequestDTO masterDataRequestDTO1 = new MasterDataRequestDTO();
            masterDataRequestDTO1.setMasterDataType("02");
            masterDataRequestDTO1.setMaterialType(factory);
            masterDataRequestDTO1.setUuid(IdUtil.fastSimpleUUID());
            masterDataRequestDTO1.setCreateTime(DateUtil.formatDate(new Date()).replace("-", ""));
            sapDockingRepeaterService.sapMaster(masterDataRequestDTO1);
        } else {
            for (int i = 0; i < beforeDay; i++) {
                MasterDataRequestDTO masterDataRequestDTO1 = new MasterDataRequestDTO();
                masterDataRequestDTO1.setMasterDataType("02");
                masterDataRequestDTO1.setMaterialType(factory);
                masterDataRequestDTO1.setUuid(IdUtil.fastSimpleUUID());
                masterDataRequestDTO1.setCreateTime(DateUtil.formatDate(getDateBeforeByNum(i)).replace("-", ""));
                sapDockingRepeaterService.sapMaster(masterDataRequestDTO1);
            }
        }
    }

    @Override
    public void syncWarehouse() {
        MasterDataRequestDTO masterDataRequestDTO1 = new MasterDataRequestDTO();
        masterDataRequestDTO1.setMasterDataType("04");
        masterDataRequestDTO1.setMaterialType(factory);
        masterDataRequestDTO1.setUuid(IdUtil.fastSimpleUUID());
        masterDataRequestDTO1.setCreateTime(DateUtil.formatDate(new Date()).replace("-", ""));
        sapDockingRepeaterService.sapMaster(masterDataRequestDTO1);
    }

    @Override
    public void syncUnit() {
        MasterDataRequestDTO masterDataRequestDTO1 = new MasterDataRequestDTO();
        masterDataRequestDTO1.setMasterDataType("06");
        masterDataRequestDTO1.setMaterialType(factory);
        masterDataRequestDTO1.setUuid(IdUtil.fastSimpleUUID());
        masterDataRequestDTO1.setCreateTime(DateUtil.formatDate(new Date()).replace("-", ""));
        sapDockingRepeaterService.sapMaster(masterDataRequestDTO1);
    }

    @Override
    public void syncMaterialGroup() {

    }

    @Override
    public void syncSupplier() {
        MasterDataRequestDTO masterDataRequestDTO1 = new MasterDataRequestDTO();
        masterDataRequestDTO1.setMasterDataType("03");
        masterDataRequestDTO1.setMaterialType(factory);
        masterDataRequestDTO1.setUuid(IdUtil.fastSimpleUUID());
        masterDataRequestDTO1.setCreateTime(DateUtil.formatDate(new Date()).replace("-", ""));
        sapDockingRepeaterService.sapMaster(masterDataRequestDTO1);
    }

    public static Date getDateBeforeByNum(Integer num) {
        Date currentDate = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentDate);
        calendar.add(Calendar.DAY_OF_YEAR, -num);
        Date previousDate = calendar.getTime();
        return previousDate;
    }

}
